package com.jdrx.sw.sewage.api.web;

import com.jdrx.platform.common.support.gateway.GwConstants;
import com.jdrx.platform.commons.rest.beans.vo.ResponseVO;
import com.jdrx.platform.commons.rest.beans.vo.ResposeVO;
import com.jdrx.platform.commons.rest.exception.BizException;
import com.jdrx.platform.commons.rest.factory.ResponseFactory;
import com.jdrx.platform.commons.rest.factory.ResponseFactory2;
import com.jdrx.sw.sewage.beans.dto.AddDTO;
import com.jdrx.sw.sewage.beans.dto.QueryDTO;
import com.jdrx.sw.sewage.beans.dto.StatisticsDTO;
import com.jdrx.sw.sewage.beans.dto.UpdateDTO;
import com.jdrx.sw.sewage.beans.entity.StationPO;
import com.jdrx.sw.sewage.beans.vo.StationNameVO;
import com.jdrx.sw.sewage.beans.vo.StationStatisticsVO;
import com.jdrx.sw.sewage.beans.vo.base.PageVO;
import com.jdrx.sw.sewage.common.constant.Constant;
import com.jdrx.sw.sewage.service.StationServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping(value = "/api/station")
@Api(value = "站点模块")
public class StationApi {
    @Autowired
    private StationServiceImpl stationServiceImpl;
    
    @ApiOperation(value = "分页站点列表")
    @PostMapping(value = "pageList")
    public ResponseVO<PageVO<StationPO>> pageList(@RequestBody @Validated QueryDTO dto) {
        return ResponseFactory2.ok(stationServiceImpl.pageList(dto));
    }
    
    @ApiOperation(value = "站点统计")
    @PostMapping(value = "statistics")
    public ResponseVO<StationStatisticsVO> statistics(@RequestBody @Validated StatisticsDTO dto) {
        return ResponseFactory2.ok(stationServiceImpl.statistics(dto));
    }

    @ApiOperation(value = "查询当前登陆人责任得站点名称")
    @RequestMapping(value = "selectStationNameList", method = RequestMethod.POST)
    public ResponseVO<List<StationNameVO>> selectStationNameList(@RequestHeader(Constant.DATA_NODE) String dataNode,
                                                                 @RequestHeader(Constant.TENANT_ID) String tenantId,
                                                                 @RequestBody List<Long> dutyUserStationIds) {
        return ResponseFactory2.ok(stationServiceImpl.selectStationNameList(dutyUserStationIds));
    }

    @ApiOperation(value = "新增站点")
    @PostMapping(value = "add")
    public ResposeVO add(@RequestBody @Validated AddDTO dto) throws BizException {
        stationServiceImpl.add(dto);
        return ResponseFactory.ok("");
    }

    @ApiOperation(value = "修改站点")
    @PostMapping(value = "update")
    public ResposeVO update(@RequestBody @Validated UpdateDTO dto,
    						@RequestHeader(Constant.TENANT_ID) Long tenantId,
    						@RequestHeader(GwConstants.TRANSPARENT_USERID_FEILD) Long uid) throws Exception {
        dto.setTenantId(tenantId);
        dto.setUid(uid);
    	stationServiceImpl.update(dto);
        return ResponseFactory.ok("");
    }
    
    @ApiOperation(value = "该接口供运营端远程调用，两边站点信息根据code同步")
    @PostMapping(value = "updateByCode")
    public ResposeVO updateByCode(@RequestBody @Validated UpdateDTO dto) throws BizException {
        stationServiceImpl.updateByCode(dto);
        return ResponseFactory.ok("");
    }
    
    @ApiOperation(value = "超级管理员获取站点信息")
    @PostMapping(value = "getLoginStation")
    public ResposeVO getLoginStation() {
        return ResponseFactory.ok(stationServiceImpl.getLoginStation());
    }
}